import {secelt} from "./select";
import mysql from "mysql";
var connection = mysql.createConnection({
    host     : 'localhost',
    user     : 'root',
    password : 'root',
    database : 'muyan'
});
connection.connect();
class sql {
    constructor(){
        this.init()
    }
    init(){
        this.table = null
        this.where = null
        this.order = null
        this.limit = null
        this.group = null
        this.data = null
        this.fields = null
        this.join = null
        this.leftJoin = null
        this.rightJoin = null
    }
    setTable(name){
        this.table = name
        return this
    }

    getAll(){
        let data = `SELECT *  FROM ${this.table}`;
        this.init()
        return this.sqlSecelt(data)
    }
    select(){
        let data = `SELECT ${this.fields || '*'} FROM ${this.table} ${this.leftJoin || ''} ${this.join || ''} ${this.rightJoin || ''} ${this.where || ''} ${this.order || ''} ${this.limit || ''}`;
        this.init()
        console.log(data)
        return this.sqlSecelt(data)
    }

    add(add){
        let keys = '';
        let values = '';
        if(add){
            if(typeof add == 'object'){
                for( let index in add){
                    keys += `${index} ,`
                    values += `'${add[index]}' ,`
                }
                keys = keys.replace(/\s+/g,"")
                keys = keys.substr(0,keys.length-1)
                values = values.replace(/\s+/g,"")
                values = values.substr(0,values.length-1)
                let data =  `INSERT INTO ${this.table} (${keys}) VALUES (${values})`
                this.init()
                return this.sqlSecelt(data)
            }
        }
    }

    update(update){
        let dataStr = ''
        if(update){
            if(typeof update == 'object'){
                for( let index in update){
                    dataStr += `${index} = '${update[index]}' ,`
                }
                dataStr = dataStr.replace(/\s+/g,"")
                dataStr = dataStr.substr(0,dataStr.length-1)
                let data =  `UPDATE ${this.table} SET  ${dataStr} ${this.where || ''} ${this.order || ''} ${this.limit || ''}`
                this.init()
                return this.sqlSecelt(data)
            }
        }
    }
    setLeftJoin(LeftJoin){
        this.leftJoin? this.leftJoin += ' LEFT JOIN ' + LeftJoin :  this.leftJoin = ' LEFT JOIN ' + LeftJoin
        return this
    }

    setJoin(join){
        this.join? this.join += ' JOIN ' + join :  this.join = ' JOIN ' + join
        return this
    }
    setRIghtJoin(rightJoin){
        this.rightJoin? this.rightJoin += ' RIGHT JOIN ' + rightJoin :  this.rightJoin = ' RIGHT JOIN ' + rightJoin
        return this
    }
    delete(){
        if(this.where){
            let data =  `DELETE FROM ${this.table} ${this.where}`
            this.init()
            return this.sqlSecelt(data)
        }
    }


    setFields(field){
        if(field) this.fields =  field;
        return this
    }

    setOrder(order){
        let orderStr = ''
        if(order) orderStr =  "ORDER BY " + order;
        this.order = orderStr
        return this
    }

    setWhere(where){
        let whereStr = '';
        if(where){
            if(typeof where == 'object'){
                for( let index in where){
                    whereStr += `${index} = ${where[index]} AND `
                }
                whereStr = `WHERE  ${whereStr}`
                whereStr = whereStr.substr(0,whereStr.length-5)
            }else{
                whereStr = `WHERE  ${where}`
            }

        }
        this.where = whereStr
        return this
    }

    setLimit(limit){
        let limitStr = '';
        if(limit){
            limitStr = "LIMIT "+limit;
        }
        this.limit = limitStr
        return this;
    }
    sqlSecelt (sql){
        return new Promise((resolve, reject) => {
            connection.query(sql, function (error, results, fields) {
                if (error){
                    resolve(error);
                } else{
                    let arr = {
                        code:200,
                        msg:'success',
                        data:results
                    }
                    resolve(arr)
                }
            });
        })
    }
}
export const  SqlApi = new sql()